/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legstar.eci.xmi;

import java.io.File;
import java.util.Properties;

import com.legstar.codegen.CodeGenMakeException;
import com.legstar.codegen.models.AbstractAntBuildModel;

/**
 * This class gathers execution parameters for the ECI XMI to COBOL-annotated
 * XML schema utility.
 * <p/>
 * The class is also capable of generating a fully configured ANT script to run
 * the ant version of the utility with the current parameter set.
 * 
 */
public class EciXmiToXsdModel extends AbstractAntBuildModel {

    /** This generator name. */
    public static final String E2X_GENERATOR_NAME = "LegStar ECI XMI to COBOL-annotated XML Schema generator";

    /** This velocity template. */
    public static final String E2X_VELOCITY_MACRO_NAME = "vlc/build-eci2xsd-xml.vm";

    /* ====================================================================== */
    /* Following are default field values. = */
    /* ====================================================================== */

    /** The default ECI WSDL input folder. */
    public static final File DEFAULT_INPUT_ECI_WSDL_FILE = new File("eci");

    /** Default encoding for ECI XMI WSDL binding files. */
    public static final String DEFAULT_ECI_WSDL_FILE_ENCODING = "UTF-8";

    /** Default filter for ECI XMI WSDL binding files. */
    public static final String DEFAULT_ECI_WSDL_FILE_FILTER = "*CICSECIBinding.wsdl";

    /** The default COBOL-annotated XML schema output folder. */
    public static final File DEFAULT_TARGET_XSD_FILE = new File("schema");

    /* ====================================================================== */
    /* Following are key identifiers for this model persistence. = */
    /* ====================================================================== */

    /** The input ECI WSDL file or folder. */
    public static final String INPUT_ECI_WSDL_FILE = "inputEciWsdlFile";

    /** The input ECI WSDL file filter. */
    public static final String INPUT_ECI_WSDL_FILE_FILTER = "inputEciWsdlFileFilter";

    /** The input ECI WSDL file encoding. */
    public static final String INPUT_ECI_WSDL_FILE_ENCODING = "inputEciWsdlFileEncoding";

    /** XSD target folder or file. */
    public static final String TARGET_XSD_FILE = "targetXsdFile";

    /* ====================================================================== */
    /* Following are this class fields that are persistent. = */
    /* ====================================================================== */

    /** The input ECI WSDL file or folder. */
    private File _inputEciWsdlFile = DEFAULT_INPUT_ECI_WSDL_FILE;

    /** The input ECI WSDL file filter. */
    private String _inputEciWsdlFileFilter = DEFAULT_ECI_WSDL_FILE_FILTER;

    /** The input ECI WSDL file encoding. */
    private String _inputEciWsdlFileEncoding = DEFAULT_ECI_WSDL_FILE_ENCODING;

    /** The target folder or file where annotated XSD will be created. */
    private File _targetXsdFile = DEFAULT_TARGET_XSD_FILE;

    /* ====================================================================== */
    /* Following are other class fields and methods. = */
    /* ====================================================================== */

    /**
     * A no-Arg constructor.
     */
    public EciXmiToXsdModel() {
        super();
    }

    /**
     * Construct from a properties file.
     * 
     * @param props the property file
     */
    public EciXmiToXsdModel(final Properties props) {
        super(props);
        setInputEciWsdlFile(getFile(props, INPUT_ECI_WSDL_FILE,
                DEFAULT_INPUT_ECI_WSDL_FILE));
        setInputEciWsdlFileFilter(getString(props, INPUT_ECI_WSDL_FILE_FILTER,
                DEFAULT_ECI_WSDL_FILE_FILTER));
        setInputEciWsdlFileEncoding(getString(props,
                INPUT_ECI_WSDL_FILE_ENCODING, DEFAULT_ECI_WSDL_FILE_ENCODING));
        setTargetXsdFile(getFile(props, TARGET_XSD_FILE,
                DEFAULT_TARGET_XSD_FILE));
    }

    /**
     * Creates an ant build script file ready for generation.
     * 
     * @param scriptFile the script file that must be created
     * @throws CodeGenMakeException if generation fails
     */
    public void generateBuild(File scriptFile) throws CodeGenMakeException {
        generateBuild(E2X_GENERATOR_NAME, E2X_VELOCITY_MACRO_NAME, scriptFile);
    }

    /**
     * The input ECI WSDL file or folder.
     * 
     * @return the input ECI WSDL file or folder
     */
    public File getInputEciWsdlFile() {
        return _inputEciWsdlFile;
    }

    /**
     * The input ECI WSDL file or folder.
     * 
     * @param inputEciWsdlFile the input ECI WSDL file or folder to set
     */
    public void setInputEciWsdlFile(final File inputEciWsdlFile) {
        _inputEciWsdlFile = inputEciWsdlFile;
    }

    /**
     * @return the input ECI WSDL file encoding
     */
    public String getInputEciWsdlFileEncoding() {
        return _inputEciWsdlFileEncoding;
    }

    /**
     * @param inputEciWsdlFileEncoding the input ECI WSDL file encoding to set
     */
    public void setInputEciWsdlFileEncoding(
            final String inputEciWsdlFileEncoding) {
        _inputEciWsdlFileEncoding = inputEciWsdlFileEncoding;
    }

    /**
     * @return the XML schema target folder or file
     */
    public File getTargetXsdFile() {
        return _targetXsdFile;
    }

    /**
     * @param targetXsdDir the target XML schema folder or file to set
     */
    public void setTargetXsdFile(final File targetXsdDir) {
        _targetXsdFile = targetXsdDir;
    }

    /**
     * @return the input ECI WSDL file filter
     */
    public String getInputEciWsdlFileFilter() {
        return _inputEciWsdlFileFilter;
    }

    /**
     * @param inputEciWsdlFileFilter the input ECI WSDL file filter to set
     */
    public void setInputEciWsdlFileFilter(String inputEciWsdlFileFilter) {
        this._inputEciWsdlFileFilter = inputEciWsdlFileFilter;
    }

    /**
     * @return a properties file holding the values of this object fields
     */
    public Properties toProperties() {
        Properties props = super.toProperties();
        putFile(props, INPUT_ECI_WSDL_FILE, getInputEciWsdlFile());
        putString(props, INPUT_ECI_WSDL_FILE_FILTER,
                getInputEciWsdlFileFilter());
        putString(props, INPUT_ECI_WSDL_FILE_ENCODING,
                getInputEciWsdlFileEncoding());
        putFile(props, TARGET_XSD_FILE, getTargetXsdFile());
        return props;
    }

}
